package com.test;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.stage.Stage;

import com.hewep.support.PrintKit;
import com.hewep.support.print.Printer;
import com.hewep.view.PrintPreviewPane;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.Sqlite3Dialect;
import com.jfinal.plugin.c3p0.C3p0Plugin;

public class AppClient extends Application implements Printable{
	private int PAGES = 1;
	private AppClient self= this;
	public static void main(String[] args) {
		launch();
		
	}

	@Override
	public void start(Stage stage) throws Exception {
		
		Group root = new Group();
		ToolBar tool = new ToolBar();
		stage.setScene(new Scene(root, 800,600));
		Button btn = new Button("print");
		btn.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent arg0) {
				// TODO Auto-generated method stub
				
				Printer print2 = PrintKit.printPreview();
				new PrintPreviewPane(print2, "00"); 
			}
		});
		root.getChildren().add(btn);
		stage.show();
	}

	@Override
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
			throws PrinterException {
		// TODO Auto-generated method stub
		 Graphics2D g2 = (Graphics2D)graphics;
        g2.setPaint(Color.black);
	    if (pageIndex > PAGES)
	        return Printable.NO_SUCH_PAGE;
        g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
        drawCurrentPageText(g2, pageFormat, pageIndex);
        return Printable.PAGE_EXISTS;
	}
	 private void drawCurrentPageText(Graphics2D g2, PageFormat pf, int page){
	        String s = "好哥哥--------\nsdfsdjfkjdskjfjsldjf";
	        String drawText;
	        float ascent = 16;
	        int k, i = 13, lines = 0;
	        while(s.length() > 0 && lines < 54)
	        {
	            k = s.indexOf('\n');
	            if (k != -1)
	            {
	                lines += 1;
	                drawText = s.substring(0, k);
	                g2.drawString(drawText, 0, ascent);
	                if (s.substring(k + 1).length() > 0)
	                {
	                    s = s.substring(k + 1);
	                    ascent += i;
	                }
	            }
	            else
	            {
	                lines += 1;
	                drawText = s;
	                g2.drawString(drawText, 0, ascent);
	                s = "";
	            }
	        }
		}
}
